package com.tools.generator.query;

import cn.hutool.core.util.StrUtil;
import com.tools.generator.enums.dbEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * MySQL 数据库查询
 * @author Admin
 */
@Slf4j
public class MySQLQuery implements AbstractQuery{

    @Override
    public dbEnum dbType() {
        return dbEnum.MySQL;
    }

    @Override
    public String tableSql(String tableName) {
        StringBuilder sql = new StringBuilder();
        sql.append("select table_name, table_comment from information_schema.tables ");
        sql.append("where table_schema = (select database()) ");
        // 表名查询
        if (StrUtil.isNotBlank(tableName)) {
            sql.append("and table_name = '").append(tableName).append("' ");
        }
        sql.append("order by table_name asc");
        log.info("执行的SQL-->:{}", sql);
        return sql.toString();
    }

    @Override
    public String tableName() {
        return "table_name";
    }

    @Override
    public String tableComment() {
        return "table_comment";
    }

    @Override
    public String tableFieldsSql() {
        return "select column_name, data_type, column_comment, column_key ,column_default,is_nullable,character_maximum_length,column_type,extra " +
                "from information_schema.columns where table_name = '%s' and table_schema = (select database()) order by ordinal_position";
    }

    @Override
    public String fieldName() {
        return "column_name";
    }

    @Override
    public String fieldType() {
        return "data_type";
    }

    @Override
    public String dataLength() {
        return "character_maximum_length";
    }

    @Override
    public String fieldComment() {
        return "column_comment";
    }

    @Override
    public String fieldKey() {
        return "column_key";
    }
}
